﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Sizebox.CharacterEditor
{
	// Token: 0x02000275 RID: 629
	public class SkeletonEditSlider : MonoBehaviour, IPointerUpHandler, IEventSystemHandler
	{
		// Token: 0x06000DEA RID: 3562 RVA: 0x0006D0AC File Offset: 0x0006B2AC
		private void Awake()
		{
			this.slider.onValueChanged.AddListener(new UnityAction<float>(this.OnSliderMove));
		}

		// Token: 0x06000DEB RID: 3563 RVA: 0x0006D0CA File Offset: 0x0006B2CA
		public void Initialize(string id, SkeletonEditSliderObject sliderObj)
		{
			this.Id = id;
			this.sliderData = sliderObj;
		}

		// Token: 0x06000DEC RID: 3564 RVA: 0x0006D0DC File Offset: 0x0006B2DC
		public void RegisterSkeleton(SkeletonEdit skeletonEdit)
		{
			this.skeletonEdit = skeletonEdit;
			int num = 0;
			foreach (SkeletonEditSliderData skeletonEditSliderData in this.sliderData.Transformations)
			{
				if (skeletonEdit.GetBone(skeletonEditSliderData.bone))
				{
					num++;
				}
			}
			this.slider.interactable = (num != 0);
			if (num == 0)
			{
				ColorBlock colors = this.slider.colors;
				colors.normalColor = this.disabledColor;
				this.slider.colors = colors;
				this.sliderBarFront.color = this.disabledColor;
				this.sliderBarBackground.color = this.disabledColor;
			}
			else if (num == this.sliderData.Transformations.Length)
			{
				ColorBlock colors2 = this.slider.colors;
				colors2.normalColor = this.normalColor;
				this.slider.colors = colors2;
				this.sliderBarFront.color = this.normalColor;
				this.sliderBarBackground.color = this.normalColor;
			}
			else
			{
				ColorBlock colors3 = this.slider.colors;
				colors3.normalColor = this.someMissingColor;
				this.slider.colors = colors3;
				this.sliderBarFront.color = this.someMissingColor;
				this.sliderBarBackground.color = this.someMissingColor;
			}
		}

		// Token: 0x06000DED RID: 3565 RVA: 0x0006D24C File Offset: 0x0006B44C
		public void OnPointerUp(PointerEventData eventData)
		{
			this.slider.onValueChanged.RemoveListener(new UnityAction<float>(this.OnSliderMove));
			this.slider.value = 0.5f;
			this.previousValue = 0.5f;
			this.slider.onValueChanged.AddListener(new UnityAction<float>(this.OnSliderMove));
		}

		// Token: 0x06000DEE RID: 3566 RVA: 0x0006D2AC File Offset: 0x0006B4AC
		private void OnSliderMove(float value)
		{
			if (!this.skeletonEdit || !this.sliderData)
			{
				return;
			}
			this.ApplySlider(value - this.previousValue);
			this.previousValue = value;
		}

		// Token: 0x06000DEF RID: 3567 RVA: 0x0006D2E4 File Offset: 0x0006B4E4
		private void ApplySlider(float change)
		{
			Humanoid component = this.skeletonEdit.GetComponent<Humanoid>();
			foreach (SkeletonEditSliderData transformation in this.sliderData.Transformations)
			{
				EditBone bone = this.skeletonEdit.GetBone(transformation.bone);
				if (bone)
				{
					this.HandleHipHeight(bone, transformation, component, change);
					this.HandleRotation(bone, transformation, component, change);
					this.HandleScaling(bone, transformation, component, change);
					this.HandleBoneLength(bone, transformation, component, change, SkeletonEditSlider.BoneLengthCallType.First);
					this.HandleMovement(bone, transformation, component, change);
				}
			}
		}

		// Token: 0x06000DF0 RID: 3568 RVA: 0x0006D388 File Offset: 0x0006B588
		private void HandleMovement(EditBone bone, SkeletonEditSliderData transformation, Humanoid target, float change)
		{
			Vector3 vector = transformation.movement * change;
			if (target.isGiantess)
			{
				vector *= 1000f;
			}
			bone.Move(this.Id, vector, transformation.options.movement, true);
		}

		// Token: 0x06000DF1 RID: 3569 RVA: 0x0006D3D8 File Offset: 0x0006B5D8
		private void HandleRotation(EditBone bone, SkeletonEditSliderData transformation, Humanoid target, float change)
		{
			Quaternion rotation = Quaternion.SlerpUnclamped(Quaternion.identity, Quaternion.Euler(transformation.rotation), change);
			bone.Rotate(this.Id, rotation, transformation.options.rotation);
		}

		// Token: 0x06000DF2 RID: 3570 RVA: 0x0006D418 File Offset: 0x0006B618
		private void HandleScaling(EditBone bone, SkeletonEditSliderData transformation, Humanoid target, float change)
		{
			Vector3 vector = transformation.scaling * change;
			vector += Vector3.one;
			bone.Scale(this.Id, vector, transformation.options.scaling);
		}

		// Token: 0x06000DF3 RID: 3571 RVA: 0x0006D45C File Offset: 0x0006B65C
		private void HandleHipHeight(EditBone bone, SkeletonEditSliderData transformation, Humanoid target, float change)
		{
			EditBone bone2 = this.skeletonEdit.GetBone(SkeletonEditBones.Hips);
			EditBone bone3 = this.skeletonEdit.GetBone(transformation.secondary);
			float num = transformation.hipHeight * change;
			if (num == 0f)
			{
				return;
			}
			Vector3 position = this.skeletonEdit.transform.position;
			if (bone3)
			{
				position = bone3.RealTransform.position;
			}
			Vector3 vector = bone.RealTransform.position - position;
			vector = this.skeletonEdit.transform.InverseTransformVector(vector) * num;
			vector.x = 0f;
			vector.z = 0f;
			SkeletonEditMovementOptions options = new SkeletonEditMovementOptions
			{
				effectPairs = false,
				speed = 1f
			};
			bone2.Move(this.Id, vector, options, true);
		}

		// Token: 0x06000DF4 RID: 3572 RVA: 0x0006D540 File Offset: 0x0006B740
		private void HandleBoneLength(EditBone bone, SkeletonEditSliderData transformation, Humanoid target, float change, SkeletonEditSlider.BoneLengthCallType callType = SkeletonEditSlider.BoneLengthCallType.First)
		{
			if (!bone || transformation.boneLength == 0f)
			{
				return;
			}
			if (transformation.boneLengthOptions.effectSibling && callType == SkeletonEditSlider.BoneLengthCallType.First)
			{
				this.HandleBoneLength(bone.SiblingBone, transformation, target, change, SkeletonEditSlider.BoneLengthCallType.Sibling);
			}
			if (callType == SkeletonEditSlider.BoneLengthCallType.First)
			{
				callType = SkeletonEditSlider.BoneLengthCallType.Child;
			}
			BoneLengthModes mode = transformation.boneLengthOptions.mode;
			EditBone bone2 = this.skeletonEdit.GetBone(transformation.secondary);
			SkeletonEditMovementOptions options = new SkeletonEditMovementOptions
			{
				effectPairs = false,
				speed = 1f
			};
			if (mode == BoneLengthModes.TargetMode)
			{
				if (!bone2)
				{
					return;
				}
				Vector3 vector = bone.RealTransform.position - bone2.RealTransform.position;
				vector *= transformation.boneLength * change;
				vector = bone.Move(this.Id, vector, options, true);
			}
			else
			{
				List<EditBone> list = SkeletonEdit.FindFirstChildEditBones(bone.RealTransform);
				foreach (EditBone editBone in list)
				{
					Vector3 vector2 = Vector3.zero;
					if (mode != BoneLengthModes.NormalMode)
					{
						if (mode == BoneLengthModes.ProportionalMode)
						{
							vector2 = editBone.RealTransform.position - bone.RealTransform.position;
							vector2 *= transformation.boneLength * change;
							vector2 = editBone.Move(this.Id, vector2, options, false);
						}
					}
					else
					{
						vector2 = (editBone.RealTransform.position - bone.RealTransform.position).normalized * (transformation.boneLength * change);
						if (target.isGiantess)
						{
							vector2 *= 1000f;
						}
						vector2 = editBone.Move(this.Id, vector2, options, true);
					}
					if (transformation.boneLengthOptions.effectChildren && editBone != bone2)
					{
						this.HandleBoneLength(editBone, transformation, target, change, callType);
					}
				}
			}
		}

		// Token: 0x040012DA RID: 4826
		private const float GIANTESS_MULT = 1000f;

		// Token: 0x040012DB RID: 4827
		[SerializeField]
		[Header("Slider Components")]
		private Slider slider;

		// Token: 0x040012DC RID: 4828
		[SerializeField]
		private Image sliderBarFront;

		// Token: 0x040012DD RID: 4829
		[SerializeField]
		private Image sliderBarBackground;

		// Token: 0x040012DE RID: 4830
		[Header("Colors")]
		[SerializeField]
		private Color someMissingColor;

		// Token: 0x040012DF RID: 4831
		[SerializeField]
		private Color normalColor;

		// Token: 0x040012E0 RID: 4832
		[SerializeField]
		private Color disabledColor;

		// Token: 0x040012E1 RID: 4833
		private SkeletonEdit skeletonEdit;

		// Token: 0x040012E2 RID: 4834
		private float previousValue = 0.5f;

		// Token: 0x040012E3 RID: 4835
		private SkeletonEditSliderObject sliderData;

		// Token: 0x040012E4 RID: 4836
		private string Id;

		// Token: 0x02000276 RID: 630
		private enum BoneLengthCallType
		{
			// Token: 0x040012E6 RID: 4838
			First,
			// Token: 0x040012E7 RID: 4839
			Sibling,
			// Token: 0x040012E8 RID: 4840
			Child
		}
	}
}
